var approvalRecordMixin = function (type) {
  var mergeTypeList = ['reimbursement', 'punishmenty', 'reward'];
  var mergeApprovalMixin = {
    data: function () {
      return {
        hoverRecordId: ''
      };
    },
    methods: {
      mergeCell: function (option) {
        var column = option.column;
        if (column.level === 2) {
          return;
        }

        var record = option.row;
        if (record.groupIndex === 0) {
          return [record.spanRow, 1];
        }
        return [0, 0];
      },

      handleMouseEnter: function (record) {
        this.hoverRecordId = record.id;
      },

      handleMouseLeave: function () {
        this.hoverRecordId = "";
      },

      setRowClassName: function (option) {
        var record = option.row;
        var rowIndex = record.rowIndex;
        var className = '';
        if (!SysUtil.isEmpty(rowIndex)) {
          className = rowIndex % 2 === 0 ? 'even' : 'odd';
        }

        var hoverRecordId = this.hoverRecordId;
        if (hoverRecordId === '') {
          return className;
        }

        if (record.reimburseInfoList.length <= 1) {
          return className;
        }
        if (record.id === hoverRecordId) {
          if (className === '') {
            className = 'hover-row';
          } else {
            className += ' hover-row';
          }
          return className;
        }
        return className;
      }
    }
  };

  var url = '';
  switch (type) {
    case 'takeoff':
      url = 'dingTalk/queryLeaveApplicationRecord';
      break;
    case 'supplementCard':
      url = 'dingTalk/queryFillcardApplicationRecord';
      break;
    case 'overtime':
      url = 'dingTalk/queryOvertimeApplicationRecord';
      break;
    case 'reimbursement':
      url = 'dingTalk/queryReimburseRecord';
      break;
    case 'punishmenty':
      url = 'dingTalk/queryAdministrativePenaltyRecord';
      break;
    case 'reward':
      url = 'dingTalk/queryAdministrativePenaltyRecord';
      break;
  }
  var mainMixin = {
    data: function () {
      var search = {
        startDate: null,
        endDate: null,
        status: '',
        deptPerson: {
          depts: [],
          persons: []
        }
      };
      return {
        loading: false,
        search: _.cloneDeep(search),
        oldSearch: _.cloneDeep(search),
        personList: [],
        records: [],
        pagination: {
          currentPage: 1,
          totalCount: 0,
          pageSize: 20
        },
        moment: moment,
        i18n: i18n
      };
    },

    created: function () {
      this.loading = true;
      var self = this;
      Promise.all([
        this.setPersonList(),
        this.setRecords(true)
      ]).then(function () {
        self.loading = false;
      }).catch(function () {
        self.loading = false;
      });
    },

    methods: {
      setPersonList: function () {
        var self = this;
        return ajaxRequest('dingTalk/askForApprover').then(function (personList) {
          personList = _.map(personList, function (person) {
            return {
              deptId: person.deptId,
              deptName: person.deptName,
              userId: person.id,
              userName: person.personName
            };
          });
          self.personList = personList;
          return personList;
        });
      },

      setRecords: function (isSearch) {
        if (!isSearch) {
          this.search = _.cloneDeep(this.oldSearch);
        }

        var search = this.search;
        var postData = this.getPostData(search);

        var pagination = this.pagination;
        var currentPage = pagination.currentPage;
        var pageSize = pagination.pageSize;
        var start = (currentPage - 1) * pageSize;
        postData.start = start;
        postData.length = pageSize;

        var self = this;
        return ajaxRequest(url, postData, false).then(function (res) {
          if (isSearch) {
            self.oldSearch = _.cloneDeep(self.search);
          }

          var records = res.returnData;
          if (SysUtil.isEmpty(records)) {
            records = [];
          }

          if (mergeTypeList.indexOf(type) !== -1) {
            records = self.getApprovalRecords(records, res);
          } else {
            self.formatRecords(records);
          }

          self.records = records;
          self.pagination.totalCount = res.iTotalRecords;
          return records;
        });
      },

      formatRecords: function () {},

      resetSearch: function () {
        this.search = _.cloneDeep(this.oldSearch);
      },

      resetRecords: function (isSearch) {
        this.loading = true;
        var self = this;
        this.setRecords(isSearch).then(function () {
          self.loading = false;
        }).catch(function () {
          self.loading = false;
        });
      },

      searchData: function () {
        this.pagination.currentPage = 1;
        this.resetRecords(true);
      },

      pageChange: function () {
        this.resetRecords();
      },

      toExportExcel: function (url) {
        var search = this.oldSearch;
        var postData = this.getPostData(search);

        var formParam = SysUtil.formatFormParam(postData, false);
        var basePath = SysUtil.getBasePath();
        var path = `${basePath}/${url}?${formParam}`;
        window.open(path);
      },

      getPostData: function (search) {
        var postData = {};

        var startDate = SysUtil.formatDate2String(search.startDate);
        var endDate = SysUtil.formatDate2String(search.endDate);
        if (startDate !== '') {
          postData.startDate = startDate;
        }

        if (endDate !== '') {
          postData.endDate = endDate;
        }

        var status = SysUtil.getDataByField(search, 'status', undefined);
        if (status !== undefined) {
          postData.status = status;
        }

        var deptPerson = search.deptPerson;
        postData.deptIds = JSON.stringify(_.map(deptPerson.depts, 'id'));
        postData.userIds = JSON.stringify(_.map(deptPerson.persons, 'id'));

        switch (type) {
          case 'punishmenty':
            postData.penaltyType = 5;
            break;
          case 'reward':
            postData.penaltyType = 6;
            break;
        }

        return postData;
      }
    },

    components: {
      SearchBtnView: SearchBtnView,
      SearchForm: ApprovalSearchForm
    }
  };

  if (mergeTypeList.indexOf(type) !== -1) {
    mainMixin.mixins = [mergeApprovalMixin];
  }

  return mainMixin;
};
